package com.example.innovate.controller;

/*
*超级管理员操作
*2020.10.23
* 2021.09.13 update
*/

import com.example.innovate.bean.Result;
import com.example.innovate.domain.AchievementCompetitionDO;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.CompetitionRefuseDO;
import com.example.innovate.service.AchieveCompetitionService;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.CompetitionRefuseService;
import com.example.innovate.service.StarPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/superAdmin")
@Api(tags = "超级管理员接口")
public class SuperAdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    CompetitionRefuseService competitionRefuseService;
    @Autowired
    AchieveCompetitionService achieveCompetitionService;
    @Autowired
    StarPointService starPointService;

    //@UserLoginToken
    @GetMapping("/findAllAdmin")
    @ApiOperation("查找所有管理员")
    public Result findAllAdmin(@RequestParam("pageNumber") Integer pageNumber,
                               @RequestParam("pageSize") Integer pageSize) throws Exception{
        List<AdminInfoDO> list = adminService.findAllAdmin(pageNumber,pageSize);
        return new Result(200,"success","返回成功",list);
    }

    //@UserLoginToken
    @PostMapping("/adminAdd")
    @ApiOperation("创建普通管理员")
    public Result add(@RequestBody AdminInfoDO adminInfoDO) throws Exception{
        String username = adminInfoDO.getAdminUserName();
        String password = adminInfoDO.getAdminPassword();
        //        校验账号是否存在
        AdminInfoDO adminByName = adminService.getAdminInfoByName(username.trim());//trim() 用于username和password的验证
        //        账号存在
        if(adminByName != null){
            return new Result(400,"error","此账号已被添加","null");
        }
        //        账号去掉前后空格后，还包含空格
        if(username.trim().contains(" ")){
            return new Result(400,"error","格式错误，不能包含空格","null");
        }
        if(username.equals("superAdmin")||username.equals("student")){
            return new Result(400,"error","用户名不能为superAdmin及student","null");
        }
        //        密码包含空格
        if(password.contains(" ")){
            return new Result(400,"error","格式错误，不能包含空格","null");
        }

        adminInfoDO.setAdminToken("null");
        adminInfoDO.setAdminIsSuperAdmin(false);
        adminInfoDO.setAdminIsAdmin(true);
        adminInfoDO.setAdminIsFrozen(false);
        adminService.addAdminInfo(adminInfoDO);
        adminInfoDO.setAdminPassword(null);
        return new Result(200,"success","创建成功",adminInfoDO);
    }

    //@UserLoginToken
    @PostMapping("/adminUpdate")
    @ApiOperation("修改普通管理员")
    public Result update(@RequestBody AdminInfoDO adminInfoDO) throws Exception{
        AdminInfoDO adminInfoDO1 = adminService.getAdminInfoById(adminInfoDO.getAdminId());
        if(adminInfoDO1!=null){
            adminService.updateAdminInfo(adminInfoDO);
            adminInfoDO1.setAdminPassword(null);
            return new Result(200,"success","修改成功",adminInfoDO);
        }
        else{
            return new Result(400,"error","管理员不存在",null);
        }

    }

    //@UserLoginToken
    @PostMapping("/adminDelete")
    @ApiOperation("删除普通管理员")
    public Result delete(@RequestParam("id") Integer id) throws Exception{
        AdminInfoDO adminInfoDO = adminService.getAdminInfoById(id);
        if(adminInfoDO!=null){
            adminService.deleteAdminInfo(id);
            return new Result(200,"success","删除成功","id="+id);
        }
        else {
            return new Result(400,"error","用户不存在","null");
        }

    }

    //@UserLoginToken
    @GetMapping("/findTempCompetition")
    @ApiOperation("获取申请竞赛获奖信息")
    public Result findTempCompetition(@RequestParam("pageNumber") Integer pageNumber,
                                      @RequestParam("pageSize") Integer pageSize) throws Exception{
        if (pageNumber == null){
            pageNumber = 1;
        }
        List<AchievementCompetitionDO> list = achieveCompetitionService.findAllExamine(pageNumber,pageSize);
        return new Result(200,"success","返回成功",list);
    }


    //@UserLoginToken
    @PostMapping("/examineCompetition")
    @ApiOperation("审核竞赛获奖信息")
    public Result examineCompetition(@RequestBody AchievementCompetitionDO achievementCompetitionDO,
                                     @RequestParam("reason") String reason,
                                     @RequestParam("status") String status) throws Exception{
        if(status.equals("refuse")){
            CompetitionRefuseDO competitionRefuseDO = new CompetitionRefuseDO();
            competitionRefuseDO.setRefuseClassName(achievementCompetitionDO.getComClassName());
            competitionRefuseDO.setRefuseReason(reason);
            competitionRefuseDO.setRefuseUserId(achievementCompetitionDO.getComUserId());
            competitionRefuseService.addRefuse(competitionRefuseDO);
            achieveCompetitionService.deleteCompetition(achievementCompetitionDO.getComId());
            return new Result(200,"success","已成功打回",competitionRefuseDO);
        }
        else if(status.equals("accept")){
            achievementCompetitionDO.setComIsTemp("false");
            achieveCompetitionService.updateCompetiton(achievementCompetitionDO);
            starPointService.addPoint();
            return new Result(200,"success","添加成功",achievementCompetitionDO);
        }
        else{
            return new Result(400,"error","状态错误",null);

        }
    }



}
